﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using SinacorWebTools.Entity;

namespace SinacorWebTools.Web
{
    public partial class Funcao : BasePage
    {
        private string Modulo
        {
            get
            {
                if (!object.ReferenceEquals(Request.QueryString["modulo"], null))
                    return Request.QueryString["modulo"].ToString();
                else
                    return string.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                btnPesquisar_Click(this, new EventArgs());
        }

        protected void Page_OnInit(object sender, EventArgs e)
        {
            srcManager.EnablePartialRendering = false;
            EnableViewState = true;
        }

        void LoadDataGrid(string pesquisa, string modulo)
        {
            SortableList<Entity.Funcao> funcoes = new Business.Funcao().ListaFuncoes(pesquisa, modulo);

            if (base.SortExpression.Length > 0)
                funcoes.Sort(base.SortExpression, (base.SortOrder == SortDirection.Ascending)); 
            
            grdFunctions.DataSource = funcoes;
            grdFunctions.DataBind();
        }

        protected void grdFunctions_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdFunctions.PageIndex = e.NewPageIndex;
            LoadDataGrid(txtPesquisa.Text, this.Modulo);
        }

        protected void grdFunctions_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:
                    ConfigGridViewHeader(grdFunctions, e.Row);
                    break;
                case DataControlRowType.DataRow:
                    Entity.Funcao f = (e.Row.DataItem as Entity.Funcao);
                    LinkButton lnkSigla = (LinkButton)e.Row.FindControl("lnkSigla");
                    if (lnkSigla != null)
                    {
                        lnkSigla.Text = f.Sigla;
                        lnkSigla.Attributes.Add("onclick", "ReturnFuncao('" + new JavaScriptSerializer().Serialize(f) + "');");
                    }
                    break;
            }
        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            base.SortExpression = string.Empty;
            grdFunctions.PageIndex = 0;
            LoadDataGrid(txtPesquisa.Text.Trim(), this.Modulo);
        }

        protected void grdFunctions_Sorting(object sender, GridViewSortEventArgs e)
        {
            base.SortExpression = e.SortExpression;

            if (base.SortOrder == SortDirection.Ascending)
                base.SortOrder = SortDirection.Descending;
            else
                base.SortOrder = SortDirection.Ascending;

            LoadDataGrid(txtPesquisa.Text, this.Modulo);
        }
    }
}
